package com.shoesdemo.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.jrummyapps.android.colorpicker.ColorPickerDialog;
import com.jrummyapps.android.colorpicker.ColorPickerDialogListener;
import com.shoesdemo.R;
import com.shoesdemo.activity.AddGoodsActivity;
import com.shoesdemo.utils.SpUtil;
import com.shoesdemo.utils.VibratorUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ColorSelectDialog extends DialogFragment implements View.OnClickListener {

    private RelativeLayout rlColor, rlBgColor;
    private TextView tvColor, tvBgColor;
    private Button mBtSure;
    private int textColor, bgColor;
    private int type;
    private RadioGroup mRadioGroup;
    private int musicType;

    public static ColorSelectDialog getInstance() {
        return new ColorSelectDialog();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View inflate = inflater.inflate(R.layout.dialog_select_item, null);
        rlColor = inflate.findViewById(R.id.rl_text_color);
        rlBgColor = inflate.findViewById(R.id.rl_bg_color);
        tvColor = inflate.findViewById(R.id.tv_text_color);
        tvBgColor = inflate.findViewById(R.id.tv_bg_color);
        mRadioGroup = inflate.findViewById(R.id.rg);
        mBtSure = inflate.findViewById(R.id.bt_sure);
        rlColor.setOnClickListener(this);
        rlBgColor.setOnClickListener(this);
        mBtSure.setOnClickListener(this);
        if (SpUtil.getTextColor() != 0) {
            textColor = SpUtil.getTextColor();
            bgColor = SpUtil.getBgColor();
        } else {
            textColor = getContext().getResources().getColor(R.color.color_white);
            bgColor = getContext().getResources().getColor(R.color.color_058AEC);
        }
        tvColor.setBackgroundColor(textColor);
        tvBgColor.setBackgroundColor(bgColor);
        musicType = SpUtil.getMusic();
        ((RadioButton) mRadioGroup.getChildAt(musicType)).setChecked(true);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int childCount = group.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    if(((RadioButton)group.getChildAt(i)).isChecked()){
                        musicType=i;
                    }
                }
                VibratorUtils.startMusic(musicType,getContext());
            }
        });
        return inflate;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_sure:
                SpUtil.setTextColor(textColor);
                SpUtil.setBgColor(bgColor);
                SpUtil.setMusic(musicType);
                getDialog().dismiss();
                break;
            case R.id.rl_text_color:
                type = 1;
                showColorPicker();
                break;
            case R.id.rl_bg_color:
                type = 2;
                showColorPicker();
                break;
        }
    }

    private ColorPickerDialogListener pickerDialogListener = new ColorPickerDialogListener() {
        @Override
        public void onColorSelected(int dialogId, @ColorInt int color) {
            if (type == 1) {
                textColor = color;
                tvColor.setBackgroundColor(color);
            } else if (type == 2) {
                bgColor = color;
                tvBgColor.setBackgroundColor(color);
            }
        }

        @Override
        public void onDialogDismissed(int dialogId) {

        }
    };

    private void showColorPicker() {
        //传入的默认color
        ColorPickerDialog colorPickerDialog = ColorPickerDialog.newBuilder()/*.setColor(0)*/
                .setDialogTitle(R.string.color_pick)
                //设置dialog标题
                .setDialogType(ColorPickerDialog.TYPE_PRESETS)
                //设置为自定义模式
                .setShowAlphaSlider(false)
                //设置有透明度模式，默认没有透明度
                .setDialogId(80)
                //设置Id,回调时传回用于判断
                .setAllowPresets(false)
                //不显示预知模式
                .create();
        //Buider创建
        colorPickerDialog.setColorPickerDialogListener(pickerDialogListener);
        //设置回调，用于获取选择的颜色
        colorPickerDialog.show(((Activity) getContext()).getFragmentManager(), "color-picker-dialog");
    }
}
